package it.mp.multitask.adapter;

import it.mp.multitask.bean.App;
import java.util.ArrayList;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class AppAdapter extends BaseAdapter {	
	private ArrayList<App> mALApp;
	private int patternRunning;
	private int patternService;
	private int patternRecently;
	private int patternPreferred;
	private BitmapDrawable bmpCestino;		

	public AppAdapter(ArrayList<App> al, int patternRunning, int patternService, int patternRecently, int patternPreferred,BitmapDrawable bmpCestino) {		
		mALApp = al;
		this.patternRunning = patternRunning;
		this.patternService = patternService;
		this.patternRecently = patternRecently;
		this.patternPreferred = patternPreferred;
		this.bmpCestino=bmpCestino;		
	}

	public View getView(int position, View convertView, ViewGroup arg2) {
		if (convertView == null) {
			try {
				App app=mALApp.get(position);
				if (app.isRunning()){
					app.setColorTextShape(patternRunning);
				}else if (app.isPreferred()){
					app.setColorTextShape(patternPreferred);
				}else if (app.isRecently()){
					app.setColorTextShape(patternRecently);
				}else if (app.isService()){
					app.setColorTextShape(patternService);
				}
				return app.getView(bmpCestino);								
			} catch (NameNotFoundException e) {
				return null;
			}
		} else {
			return convertView;
		}
	}

	public int getCount() {
		return mALApp.size();
	}

	public Object getItem(int position) {
		return mALApp.get(position);
	}

	public long getItemId(int position) {
		return position;
	}


}
